// @ts-nocheck
const depthFirstSearch = <T = any>(
  node: T,
  fn: (node: T, level: number) => any,
  childrenKey: string | undefined = 'children'
) => {
  const stack = [{ node, level: 0 }];
  while (stack.length > 0) {
    const { node, level } = stack.pop() || {};
    if (node) {
      fn(node, level);
      let childrenArr = [];
      if (typeof node[childrenKey] === 'function') {
        childrenArr = node[childrenKey]();
      } else {
        childrenArr = node[childrenKey];
      }
      if (childrenArr?.length) {
        for (let i = childrenArr.length - 1; i >= 0; i--) {
          stack.push({ node: childrenArr[i], level: level + 1 });
        }
      }
    }
  }
};

function  isOpNodeByDom(dom: any): boolean {
    const self = dom?.dataset.visdata
    const str = dom?.parentElement?.dataset.visdata
    return (
      self || str
    );
}
export function mapComTree(dom) {
  const newList: any[] = [];
  depthFirstSearch(
      dom,
      (itemDom) => {
          if (isOpNodeByDom(itemDom)) {
              newList.push(itemDom);
          }
      },
      'children'
  );

  return newList
}

export function mapTree(node, fn) {
  depthFirstSearch(node, fn, 'children');
}
